<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Download
{
    private $agent;

    public function setAgent($val)
    {
        $this->agent = $val;
    }

    public function exec($aData)
    {
    	$arr = array();
        $curl_handle = curl_init();
        curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($curl_handle, CURLOPT_TIMEOUT, 1000);
        curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl_handle, CURLOPT_USERAGENT, $this->agent);
        foreach ($aData as $item) {
            if ($item['remark']) {
                curl_setopt($curl_handle, CURLOPT_URL, $item['url']);
                curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $item['queryString']);
            } else {
                curl_setopt($curl_handle, CURLOPT_URL, $item['url'] . $item['queryString']);
            }
            $returnData = trim(curl_exec($curl_handle));
            if (strpos($returnData, ERR_CSV_NORMAL) === false && strpos($returnData, ERR_CSV_CNX) === false && strpos($returnData, NO_DATA_FOUND) === false) {
                $res = file_put_contents(TEMP_CSV_PATH . $item['symbol'] . TEMP_CSV_FILE_EXT, $returnData);
                if ($res > 0) {
                    array_push($arr, $item['symbol']);
                }
            }
        }
        curl_close($curl_handle);
        return $arr;
    }
}
?>